﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Interop;
using System.Windows.Threading;
using System.Diagnostics;
using System.Windows.Controls;

namespace MasterDetailEx
{
    /// <summary>
    /// Lógica de interacción para App.xaml
    /// </summary>
    public partial class App : Application
    {
        private const string Unique = "TOOLBOX_Unique_Application_String";
        private static MasterDetailEx.App app;

        public App()
        {
            //Validar conexión con base de datos
            BAL.EstRec.Common.DatabaseChecker dc = new BAL.EstRec.Common.DatabaseChecker();
            if (!dc.checkLocalDB())
            {
                MessageBox.Show(dc.LocalErrorMessage, "Base local", MessageBoxButton.OK, MessageBoxImage.Stop);
                this.Shutdown();
                return;
            }
        }

        protected override void OnStartup(StartupEventArgs e)
        {

            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
            base.OnStartup(e);
        }

        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            // Prevent default unhandled exception processing
            e.Handled = true;

            // Process unhandled exception
            //Mdb.setMsgAppLog(22, "Show win " + e.Exception.Message.ToString(), String.Format("{0:s}", DateTime.Now), 1);
            BAL.EstRec.AppConfs cnfs = new BAL.EstRec.AppConfs();

            try
            {
                MessageBox.Show("Ha ocurrido un error general " + " | [" + e.Exception.Message.ToString() + "]. ToolBox se cerrará"
                , "ToolBox", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                try
                {
                    var main = App.Current.MainWindow as MainWindow;
                    main.DTimerClock.Stop();
                    main.DTimerClock1.Stop();
                    Process p = new Process();
                    p.StartInfo = new ProcessStartInfo();
                    p.StartInfo.UseShellExecute = true;
                    p.StartInfo.FileName = "taskkill.exe";
                    p.StartInfo.Arguments = "/F /IM " + cnfs.cnf.appName;
                    p.Start();
                    p.WaitForExit();
                }
                catch (Exception ex)
                {
                    App.Current.Shutdown(0);
                }
            }
        }

        #region ISingleInstanceApp Members

        public bool SignalExternalCommandLineArgs(IList<string> args)
        {
            // handle command line arguments of second instance
            // ...
            MessageBox.Show("ToolBox ya está en ejecución");
            return true;
        }

        #endregion
    }// end class
}
